import PageFilterComponent from '@/components/PageFilter/index.vue';
import { EnterpriseApi } from '@/api';
import { mapGetters } from 'vuex';

export default {
  name: 'EnterpriseList',
  components: { PageFilterComponent },
  data() {
    return {
      tableData: {
        page: 1,
        pageSize: 20,
        list: [],
      },
      filterForm: {
        company_name: '',
      },
    };
  },
  created() {
    this.fetchTableData();
  },
  computed: {
    ...mapGetters(['user']),
  },
  methods: {
    async fetchTableData() {
      try {
        const response = await EnterpriseApi.page({
          page: this.tableData.page,
          pageSize: this.tableData.pageSize,
          ...this.filterForm,
        });
        this.tableData = response.result;
      } catch (e) {
        this.$message.error('查询失败：' + JSON.stringify(e))
      }
    },
    onStatusTabClick() {
      this.setFilter();
    },
    toEnterpriseDetail(type, id) {
      const baseUrl = '/enterprise/detail';
      switch (type) {
        case 'ADD':
          this.$router.push({ path: baseUrl });
          break;
        case 'EDIT':
          this.$router.push({ path: baseUrl, query: { id } });
          break;
        case 'VIEW':
          this.$router.push({ path: baseUrl, query: { id, preivew: 1 } });
          break;
        default:
          break;
      }
    },
    onPageChanged(page) {
      this.tableData.page = page;
      this.fetchTableData();
    },
    onPageSizeChanged(pageSize) {
      this.tableData.pageSize = pageSize;
      this.fetchTableData();
    }
  },
};
